﻿CREATE FUNCTION [dbo].[SemiMonthlyPaymentSchedule_Old](
	@StartDate datetime,
	@Today datetime
)RETURNS  @T TABLE (PaymentDate datetime) WITH SCHEMABINDING AS BEGIN

DECLARE @IsFirstDay bit SET @IsFirstDay = CASE WHEN DATEPART(dd,@StartDate) < 15 THEN 1 ELSE 0 END
DECLARE @FirstDate datetime SET @FirstDate = CASE @IsFirstDay WHEN 1 THEN @StartDate ELSE DATEADD(dd,-15,@StartDate) END

WHILE DATEPART(mm,@FirstDate) < DATEPART(mm,@StartDate) SET @FirstDate = DATEADD(dd,1,@FirstDate)


WHILE @StartDate <= @Today BEGIN
	INSERT INTO @T VALUES(@StartDate)
	IF @IsFirstDay = 1 SET @FirstDate = @StartDate
	SELECT @StartDate = CASE @IsFirstDay WHEN 1 THEN DATEADD(dd,15,@StartDate) ELSE DATEADD(mm,1,@FirstDate) END,
				 @IsFirstDay = @IsFirstDay ^ 1
	--IF @StartDate > @Today BREAK
END

RETURN
END


